package com.tjhzzx.danfossNiagaraCore.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Luc
 * @version 1.0
 * @date 2022-09-23 10:53
 */
public class EnergyUtil {
    public static String[] getStatisticsTimeRange(String startTime, String endTime, int peroid, int amount, boolean bCost) throws ParseException {
        if(StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return null;
        }
        String[] times = new String[2];
        Calendar calendar = new GregorianCalendar();
        if(0 == peroid) {
            if(!bCost) {
                startTime += " 00:00:00";
            } else {
                String[] startTimes = startTime.trim().split("-");
                startTime = startTimes[0] + "-" + startTimes[1] + "-01 00:00:00";
            }
            Date endDate = TlsUtil.string2utilDate(endTime, "yyyy-MM-dd");
            calendar.setTime(endDate);
            calendar.add(Calendar.DATE, amount);
            endDate = calendar.getTime();
            endTime = TlsUtil.utilDate2String(endDate, "yyyy-MM-dd", false);
            endTime += " 00:00:00";
        } else if(1 == peroid) {
            startTime += "-01 00:00:00";
            Date endDate = TlsUtil.string2utilDate(endTime, "yyyy-MM");
            calendar.setTime(endDate);
            calendar.add(Calendar.MONTH, amount);
            endDate = calendar.getTime();
            endTime = TlsUtil.utilDate2String(endDate, "yyyy-MM", false);
            endTime += "-01 00:00:00";
        } else if(2 == peroid) {
            startTime += "-01-01 00:00:00";
            Date endDate = TlsUtil.string2utilDate(endTime, "yyyy");
            calendar.setTime(endDate);
            calendar.add(Calendar.YEAR, amount);
            endDate = calendar.getTime();
            endTime = TlsUtil.utilDate2String(endDate, "yyyy", false);
            endTime += "-01-01 00:00:00";
        }
        times[0] = startTime;
        times[1] = endTime;
        return times;
    }
}
